//
//  HomeViewController.m
//  Yu01
//
//  Created by 蒋松涵 on 16/8/19.
//  Copyright © 2016年 蒋松涵. All rights reserved.
//

#import "HomeViewController.h"
//选课
#import "XuanKeViewController.h"
//课表
#import "schoolTimetableViewController.h"
//签到
#import "AttendanceVC.h"
//教室
#import "ClassroomInquireVC.h"
//
#import "TongzhiViewController.h"

#import "LXSegmentScrollView.h"

//
#import "SystemAnnouncement.h"
#import "SelectImageForIpone.h"
#import "UIImageView+WebCache.h"

@interface HomeViewController ()
{
    UITableView * _tableView;
    NSArray * _imageViewArray;
    NSArray * _labelArray;
    NSArray * _titleArr;
    UIView*vi_;

}
//滚动视图对象
@property (retain, nonatomic) UIScrollView *scrollView;
//
@property(nonatomic,strong)NSMutableArray * dataArray;

//视图中小圆点，对应视图的页码
@property (retain, nonatomic) UIPageControl *pageControl;
//动态数组对象，存储图片
@property (retain, nonatomic) NSMutableArray *images;

@property (nonatomic,retain) NSString *urlStr;
@property (nonatomic,retain) NSDictionary *urlParameters;


@property (nonatomic,strong)NSString * Logo;


@end
@implementation HomeViewController



//请求数据
- (void)fecthDataFromNet:(NSString *)url parameters:(NSDictionary *)parameter {
    
    NSLog(@"%@",url);
    NSLog(@"%@",parameter);
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

        NSLog(@"~~~~%@",dic);
        NSMutableArray * List = dic[@"rows"];
        for (NSDictionary * dicList in List)
        {
            SystemAnnouncement * model = [[SystemAnnouncement alloc]init];
            [model setValuesForKeysWithDictionary:dicList];
            self.Logo = model.logo;
            
            [self.dataArray addObject:model];
            NSLog(@"----%ld",self.dataArray.count);
        }
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        

    _urlStr = @"http://120.76.241.179/app/noticeInfo/listJson";
    _urlParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"appWebConfigId",@"notice",@"noticeInfo. noticeTypeCode",@"1",@"page",@"10",@"rows",@"1",@"noticeInfo. recommend", nil];
    [self fecthDataFromNet:_urlStr parameters:_urlParameters];

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];

    self.dataArray = [[NSMutableArray alloc]init];
    
    
    //导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //修改导航栏添加的按钮（item）颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    [self scrollview];
    
    [self collection];
//    [self viewControllerSeg];
    [self viewcontroller];
    
}

#pragma mark - ScrollView
-(void)scrollview
{
    
    CGFloat h = 736 / ScreenHeight;
    CGFloat w = 414 / ScreenWidth;
    
    // 自动滚动调整,默认为YES
    self.automaticallyAdjustsScrollViewInsets = NO;

    //初始化scrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64/h, ScreenWidth, 200/h)];
    //实际大小
    self.scrollView.contentSize = CGSizeMake(ScreenWidth * 3, 200);
    //是否分页显示
    self.scrollView.pagingEnabled = YES;
    //禁止垂直滚动
    self.scrollView.scrollEnabled = YES;
    //
    for (int i = 0; i < 3; i ++)
    {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth * i, 0, ScreenWidth, 140/h)];
        NSString * name = [NSString stringWithFormat:@"校话补充图标-04"];
        imageView.image = [UIImage imageNamed:name];
        imageView.backgroundColor = [UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1];
        [self.scrollView addSubview:imageView];
    }
    //
    self.scrollView.delegate = self;
    
    //把scrollView与pageControl添加到当前视图中
    [self.view addSubview:self.scrollView];
    //初始化pageControl
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(30/w, 170/h, ScreenWidth - 35/w, 36/h)];
    //设置页面个数
    self.pageControl.numberOfPages = 3;
    //设置当前页
    self.pageControl.currentPage = 0;
    //
    self.pageControl.pageIndicatorTintColor = [UIColor greenColor];
    //
    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    //
    [self.pageControl addTarget:self action:@selector(pchange) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.pageControl];
    //设置视图的背景颜色
    self.scrollView.backgroundColor = [UIColor blackColor];
}
#pragma mark - CollectionView
-(void)collection
{
    CGFloat h = 736 / ScreenHeight;
    CGFloat w = 414 / ScreenWidth;
    
    
    
    // 流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置cell的尺寸
    CGFloat screecW = ScreenWidth;
    //    layout.itemSize = CGSizeMake(50, 50);
    
    //每个cell的间距
    CGFloat minimum = 1;
    //一行cell 的个数
    CGFloat count = 2;
    //每一行的上下距离
    CGFloat minimum2 = 2;
    //每个cell的宽
    CGFloat cellW = (screecW  - minimum *count - minimum)/count ;
    //每个cell的高
    CGFloat cellH = cellW/3;
    layout.itemSize = CGSizeMake(cellW, cellH);
    
    // 设置每一行的间距
    layout.minimumLineSpacing = minimum2;
    
    // 设置每个cell的间距
    layout.minimumInteritemSpacing = minimum;
    
    UICollectionView *collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 200/h, ScreenWidth, 150/w) collectionViewLayout:layout];
    collectionView.backgroundColor =[UIColor whiteColor];
    collectionView.delegate =self;
    collectionView.dataSource =self;
    collectionView.backgroundColor = [UIColor colorWithRed:231/255.0 green:235/255.0 blue:235/255.0 alpha:1];
    [self.view addSubview:collectionView];
    
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

#pragma mark - 按钮滚动
-(void)viewcontroller
{
    CGFloat h = 736 / ScreenHeight;
    CGFloat w = 414 / ScreenWidth;
    
    
    NSMutableArray *array=[NSMutableArray array];
//    for (int i =0; i<2; i++) {
//        UIView *view=[[UIView alloc] init];
//        if (i==0) {
//            view.backgroundColor=[UIColor brownColor];
//        }if (i==1) {
//            view.backgroundColor=[UIColor greenColor];
//        }
//        [array addObject:view];
//        
//    }
    
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 390/w, ScreenWidth, ScreenHeight - 440 / h) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.rowHeight=100/h;
    
//    [array addObject:_tableView];
    [self.view addSubview:_tableView];
    
    LXSegmentScrollView *scView=[[LXSegmentScrollView alloc] initWithFrame:CGRectMake(0, 350/w, ScreenWidth,40/h) titleArray:@[@"公告",@"新闻"] contentViewArray:array];
    [self.view addSubview:scView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h = 736 / ScreenHeight;
    CGFloat w = 414 / ScreenWidth;
    
    static NSString * CellIdentifier = @"Cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    SystemAnnouncement * model = self.dataArray[indexPath.row];
    
//    cell.imageView.image = [UIImage imageNamed:@"jiaju.jpg"];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 150, cell.frame.size.height)];
    
    NSString * str1 = [NSString stringWithFormat:@"%@%@",APP_URL,self.Logo];
    NSLog(@"???????_%@",[NSString stringWithFormat:@"%@%@",APP_URL,self.Logo]);
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:str1]];
    NSLog(@"111111%@",str1);
    
    [cell.contentView addSubview:imageView];
    
//    NSString * str1 = [NSString stringWithFormat:@"%@%@",APP_URL,self.Logo];
//
//        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:str1] placeholderImage:[UIImage imageNamed:@""]];
//    NSLog(@"111111%@",str1);
    
    
    UILabel * GGLabel = [[UILabel alloc]initWithFrame:CGRectMake(200/w, 20/h, 150/w, 30/h)];
//    GGLabel.text = @"公告";
    GGLabel.text = model.title;
    GGLabel.font = [UIFont systemFontOfSize:14/w];
    [cell.contentView addSubview:GGLabel];
    
    
    UILabel * FBTLabel = [[UILabel alloc]initWithFrame:CGRectMake(200/w, 70/h, 200/w, 30/h)];
//    FBTLabel.text = @"西安工程大学2016年研究生";
    FBTLabel.text = model.summary;
    FBTLabel.font = [UIFont systemFontOfSize:13/w];
    [cell.contentView addSubview:FBTLabel];
    
    UILabel * TimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width - 50/w, 40/h, 150/w, 30/h)];
//    TimeLabel.text = @"12:50";
    TimeLabel.text = model.createTime;
    TimeLabel.font = [UIFont systemFontOfSize:12/w];
    [cell.contentView addSubview:TimeLabel];
    
    
    return cell;
}

#pragma mark - UIScrollViewDelegate
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    //只要滚动了就会触发
//    if ([scrollView isKindOfClass:[UITableView class]]) {
//        
//    }
//    else
//    {
//        [self changeView:self.MenuscrollView.contentOffset.x];
//    }
//}

#pragma mark - TableView

#pragma mark - tableView的代理方法

-(void)pchange
{
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.pageControl.currentPage * self.view.frame.size.width, 0);
    }];
}

#pragma mark - 表头
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 2;
//}

#pragma mark - UICollectionViewDelegate------
// 返回有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

// 返回每个cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    CGFloat h = 736 / ScreenHeight;
//    CGFloat w = 414 / ScreenWidth;
    
    UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    //颜色随机
//    UIColor *color =[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
//    cell.backgroundColor =color;
    cell.backgroundColor = [UIColor colorWithRed:254/255.0 green:255/255.0 blue:255/255.0 alpha:1];
    switch (indexPath.row)
    {
        case 0:
        {
            //签到
            UILabel * qianLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 30)];
            qianLabel.text = @"签到";
            [cell.contentView addSubview:qianLabel];
            UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 80, 30)];
            label1.text = @"上课签到";
            label1.font = [UIFont systemFontOfSize:12];
            [cell.contentView addSubview:label1];
            
            UIImageView * images = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width - 70, 15, 35, 35)];
            images.image = [UIImage imageNamed:@"主界面图标导图-01 128"];
            [cell.contentView addSubview:images];
        }
            break;
        case 1:
        {
            //选课
            UILabel * qianLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 30)];
            qianLabel.text = @"课表";
            [cell.contentView addSubview:qianLabel];
            UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 80, 30)];
            label1.text = @"课表查询";
            label1.font = [UIFont systemFontOfSize:12];
            [cell.contentView addSubview:label1];
            
            UIImageView * images = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width - 70, 15, 35, 35)];
            images.image = [UIImage imageNamed:@"主界面图标导图-04"];
            [cell.contentView addSubview:images];
        }
            break;
        case 2:
        {
            //课表
            UILabel * qianLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 30)];
            qianLabel.text = @"选课";
            [cell.contentView addSubview:qianLabel];
            UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 80, 30)];
            label1.text = @"在线选课";
            label1.font = [UIFont systemFontOfSize:12];
            [cell.contentView addSubview:label1];
            
            UIImageView * images = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width - 70, 15, 35, 35)];
            images.image = [UIImage imageNamed:@"主界面图标导图-07"];
            [cell.contentView addSubview:images];
        }
            break;
        case 3:
        {
            //选课
            UILabel * qianLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 30)];
            qianLabel.text = @"查寝";
            [cell.contentView addSubview:qianLabel];
            UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 80, 30)];
            label1.text = @"睡前查寝";
            label1.font = [UIFont systemFontOfSize:12];
            [cell.contentView addSubview:label1];
            
            UIImageView * images = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width - 70, 15, 35, 35)];
            images.image = [UIImage imageNamed:@"主界面图标导图-10"];
            [cell.contentView addSubview:images];
        }
            break;
        default:
            break;
    }
    
    return cell;
}
//每一个cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            //签到
//            self.hidesBottomBarWhenPushed = YES;
//            AttendanceVC * attendVC = [[AttendanceVC alloc]init];
//            [self.navigationController pushViewController:attendVC animated:YES];
//            self.hidesBottomBarWhenPushed = NO;
        }
            break;
        case 1:
        {
            //课表
            self.hidesBottomBarWhenPushed = YES;
            schoolTimetableViewController * scheduleVC = [[schoolTimetableViewController alloc]init];
            [self.navigationController pushViewController:scheduleVC animated:YES];
            self.hidesBottomBarWhenPushed = NO;
        }
            break;
        case 2:
        {
            self.hidesBottomBarWhenPushed=YES;
            //选课
            XuanKeViewController * VC = [[XuanKeViewController alloc]init];
            [self.navigationController pushViewController:VC animated:YES];
            self.hidesBottomBarWhenPushed=NO;
        }
            break;
        case 3:
        {
            //查寝
//            self.hidesBottomBarWhenPushed = YES;
//            ClassroomInquireVC * classroomVC = [[ClassroomInquireVC alloc]init];
//            [self.navigationController pushViewController:classroomVC animated:YES];
//            self.hidesBottomBarWhenPushed = NO;
        }
            break;
        default:
            break;
    }

}

#pragma mark-----UIScrollViewDelegate---------
//实现协议UIScrollViewDelegate的方法，必须实现的
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //获取当前视图的宽度
    CGFloat pageWith = scrollView.frame.size.width;
    //根据scrolView的左右滑动,对pageCotrol的当前指示器进行切换(设置currentPage)
    int page = floor((scrollView.contentOffset.x - pageWith/2)/pageWith)+1;
    //切换改变页码，小圆点
    self.pageControl.currentPage = page;
    
    
}

#pragma mark - TableView代理协议的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SystemAnnouncement * model = self.dataArray[indexPath.row];
    
    //
    self.hidesBottomBarWhenPushed = YES;
    TongzhiViewController * tongzhiVC = [[TongzhiViewController alloc]init];
    
    tongzhiVC.NotURL = [NSString stringWithFormat:@"http://www.100xsys.com.cn/school/app/noticeInfo/viewUI?noticeInfo.id=%@",model.id];
    
    [self.navigationController pushViewController:tongzhiVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
